$(document).ready(function(){
  $("#page_del").click(function(){
    var page_id=$(this).attr('rel');
    $("#page_edit_div").slideUp("slow");
    if (confirm('Bu Sayfayı Silmek İstediğinize Emin misiniz?')) {
      $.ajax({
      	data:"do=pagedel&id="+page_id,
      	success: function(r) { alert(r); },
      	error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert(textStatus);
        }
      });
    }
  });

  $("#page_edit").click(function() {
    var page_id=$(this).attr('rel');
    var page_div=$("#page_edit_div");
    if (page_div.text()=='') {
      page_div.slideDown("slow");
      $.ajax({
      	data:"do=pageedit&id="+page_id,
      	success: function(r) {
      	  page_div.hide();
          r = r.replace(/\r\n/g,"\\n");
          r = r.replace(/\n/g,"\\n");
          r = r.replace(/\r/g,"\\r");
      	  eval(r);
      	  page_div.slideDown("slow");
      	},
      	error: function (XMLHttpRequest, textStatus, errorThrown) {
          page_div.hide();
          alert(textStatus);
        }
      });
    }
    else page_div.slideToggle("slow");
  });

/**************************************************************************/

  $("#cat_del").click(function(){
    var cat_id=$(this).attr('rel');
    $("#cat_edit_div").slideUp("slow");
    if (confirm('Bu Kategoriyi Silmek İstediğinize Emin misiniz?')) {
      $.ajax({
      	data:"do=catdel&id="+cat_id,
      	success: function(r) { alert(r); },
      	error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert(textStatus);
        }
      });
    }
  });

  $("#cat_edit").click(function() {
    var cat_id=$(this).attr('rel');
    var cat_div=$("#cat_edit_div");
    if (cat_div.text()=='') {
      cat_div.slideDown("slow");
      $.ajax({
      	data:"do=catedit&id="+cat_id,
      	success: function(r) {
      	  cat_div.hide();
          r = r.replace(/\r\n/g,"\\n");
          r = r.replace(/\n/g,"\\n");
          r = r.replace(/\r/g,"\\r");
      	  eval(r);
      	  cat_div.slideDown("slow");
      	},
      	error: function (XMLHttpRequest, textStatus, errorThrown) {
          cat_div.hide();
          alert(textStatus);
        }
      });
    }
    else cat_div.slideToggle("slow");
  });
  /**************************************************************************/

  $("#extable img[title='Sil']").each(function(){
    $(this).click(function(){
      if (confirm('Bu kuru silmek istediğinize emin misiniz?')) {
        var ex_id=$(this).attr('alt');
        $.ajax({
        	data:"do=exdel&id="+ex_id,
        	success: function(r) { eval(r); },
        	error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
          }
        });
      }
    });
  });

  $("#extable img[title='Düzenle']").each(function(){
    $(this).click(function(){
      var ex_id=$(this).attr('alt');
      $.ajax({
      	data:"do=exedit&id="+ex_id,
      	success: function(r) {
          r = r.replace(/\r\n/g,"\\n");
          r = r.replace(/\n/g,"\\n");
          r = r.replace(/\r/g,"\\r");
      	  eval(r);
      	},
      	error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert(textStatus);
        }
      });
    });
  });
  /**************************************************************************/

  $("#brandtable img[title='Sil']").each(function(){
    $(this).click(function(){
      if (confirm('Bu markayı silmek istediğinize emin misiniz?')) {
        var ex_id=$(this).attr('alt');
        $.ajax({
        	data:"do=branddel&id="+ex_id,
        	success: function(r) { eval(r); },
        	error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
          }
        });
      }
    });
  });

  $("#brandtable img[title='Düzenle']").each(function(){
    $(this).click(function(){
      var ex_id=$(this).attr('alt');
      $.ajax({
      	data:"do=brandedit&id="+ex_id,
      	success: function(r) {
          r = r.replace(/\r\n/g,"\\n");
          r = r.replace(/\n/g,"\\n");
          r = r.replace(/\r/g,"\\r");
      	  eval(r);
      	},
      	error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert(textStatus);
        }
      });
    });
  });
  /**************************************************************************/
  $("#priceadd").click(function(){
    $("#pricebutton").val(" Fiyat Ekle ");
    $("#what").val("add");
    $("#price_id").val("0");
    $("#price").val("");
    $("#pinfo").val("");
    $("#campaign_price").val("");
    $("#vat").val("18");
    $("#pxml_code").val("");
    $("#pactive").attr("checked","checked");
    $("#campaign").attr("checked","");

    $("#edit").hide();
    $("#itemphotoadd").hide();
    $("#itempriceadd").toggle("slow");
  });

  $("#photoadd").click(function(){
    $("#edit").hide();
    $("#itempriceadd").hide();
    $("#itemphotoadd").toggle("slow");
  });

  $("a[name='img_del']").each(function(){
    $(this).click(function(){
    $("#ajaxloding").show();
    if (confirm("Ürünü fotoğrafını silmek istediğinize emin misiniz?")) {
      $.ajax({
      	data:"do=img_del&id="+ $(this).attr('id'),
      	success: function(r) {
      	  eval(r);
      	  $("#ajaxloding").hide();
      	},
      	error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert(textStatus);
        }
      });
    }
    });
  });

  $("#itemdel").click(function(){
    $("#ajaxloding").show();
    $("#itemphotoadd").hide();
    $("#itempriceadd").hide();
    $("#edit").hide();
    if (confirm("Ürünü silmek istediğinize emin misiniz?")) {
      $.ajax({
      	data:"do=itemdel&id="+$("#itemdel").attr('name'),
      	success: function(r) {
      	  alert(r);
      	},
      	error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert(textStatus);
        }
      });
    }
    $("#ajaxloding").hide();
  });

  $("#itemedit").click(function(){
    $("#itemphotoadd").hide();
    $("#itempriceadd").hide();
    $("#edit").toggle("slow");
  });
});

/*#########################################################################*/

function saveForm(formID,what) {
  $("#"+what+"_edit_div").slideUp("slow");
  var form=$("#"+formID).serialize();
  $.ajax({
  	data:"do="+what+"update&"+form,
  	success: function(r) {
  	  alert(r);
  	},
  	error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert(textStatus);
    }
  });
}

function editItem() {
  $("#ajaxloding").show();
  $("#edit").slideUp();
  $.ajax({
    data:"do=itemedit&"+$("#itemeditForm").serialize(),
    success: function(r) { alert(r); $("#edit").slideUp(); },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert(textStatus);
      $("#edit").slideDown();
    }
  });
  $("#ajaxloding").hide();
  return false;
}

function catSelect(catid) {
  var div='<div class="catselect" id="selectdiv"></div>';
  if ($("#selectdiv").text()=='') $("#icerik").append(div);
  $.ajax({
  	data:"do=catselect&catid="+catid,
  	success: function(r) {
      r = r.replace(/\r\n/g,"\\n");
      r = r.replace(/\n/g,"\\n");
      r = r.replace(/\r/g,"\\r");
  	  eval(r);
  	},
  	error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert(textStatus);
    }
  });
}

function photoSelect(start) {
  var div='<div class="catselect" id="photoselect"></div>';
  if ($("#photoselect").text()=='') $("#icerik").append(div);
  $.ajax({
  	data:"do=photoselect&start="+start,
  	success: function(r) {
      r = r.replace(/\r\n/g,"\\n");
      r = r.replace(/\n/g,"\\n");
      r = r.replace(/\r/g,"\\r");
  	  eval(r);
  	},
  	error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert(textStatus);
    }
  });
}

function setImage(image) {
  $("#images").append('<img src="template/img/delete.gif" title="Sil" class="adminicon" onclick="photoDel(\''+image+'\');" alt="'+image+'" /> ');
  $("#images").append('<a rel="shadowbox;" title="Fotoğraf" href="img/big/'+image+'" alt="'+image+'"">'+image+'</a><br />');
  Shadowbox.setup();
}

function photoDel(image) {
  $.ajax({
  	data:"do=photodel&image="+image,
  	success: function(r) {
      r = r.replace(/\r\n/g,"\\n");
      r = r.replace(/\n/g,"\\n");
      r = r.replace(/\r/g,"\\r");
  	  eval(r);
  	},
  	error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert(textStatus);
    }
  });
}

function makeSelection(id) {
  var check=0;
  if ($("#choice_"+id).attr('checked')) check=1;
  $.ajax({
  	data:"do=makeselection&id="+id+"&check="+check,
  	success: function(r) {
      r = r.replace(/\r\n/g,"\\n");
      r = r.replace(/\n/g,"\\n");
      r = r.replace(/\r/g,"\\r");
  	  eval(r);
  	},
  	error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert(textStatus);
    }
  });
}

function addPrice(itemID) {
  $("#ajaxloding").show();
  $("#itempriceadd").slideUp();
  var form=$("#priceForm").serialize();
  $.ajax({
  	data:"do=itemprice&id="+itemID+"&"+form,
  	success: function(r) {
      r = r.replace(/\r\n/g,"\\n");
      r = r.replace(/\n/g,"\\n");
      r = r.replace(/\r/g,"\\r");
  	  eval(r);
  	},
  	error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert(textStatus);
      $("#itempriceadd").slideDown();
    }
  });
  $("#ajaxloding").hide();
  return false;
}

function removePrice(pid) {
  $("#ajaxloding").show();
  $("#itemphotoadd").hide();
  $("#itempriceadd").hide();
  $("#edit").hide();
  if (confirm("Bu fiyatı silmek istediğinize emin misiniz?")) {
    $.ajax({
    	data:"do=pricedel&id="+pid,
    	success: function(r) { eval(r); },
    	error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus);
      }
    });
  }
  $("#ajaxloding").hide();
}

function editPrice(pid) {
  $("#ajaxloding").show();
  $("#edit").hide();
  $("#itemphotoadd").hide();
  $("#itempriceadd").slideUp();
  $.ajax({
  	data:"do=priceedit&id="+pid,
  	success: function(r) { eval(r); },
  	error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert(textStatus);
      $("#itempriceadd").slideDown();
    }
  });
}

function delUsers() {
  var send=false;
  var sendData='do=userdel';
  $("input[name='user']:checked").each(function(){
    send=true;
    var user_id=$(this).attr('value');
    sendData=sendData+"&user[]="+user_id;
  });
  if (send) {
    if(confirm('Seçilen Kullanıcıların Tüm Verilerini Silmek İstediğinize Emin misiniz?')) {
      $.ajax({
      	data:sendData,
      	success: function(r) { eval(r); },
      	error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert(textStatus);
        }
      });
    }
  }
}

function changeLevel(){
  var send=false;
  var sendData="do=changelevel&level="+$("#level").val();
  $("input[name='user']:checked").each(function(){
    send=true;
    var user_id=$(this).attr('value');
    sendData=sendData+"&user[]="+user_id;
  });
  if (send) {
    if(confirm('Seçilen Kullanıcıların Seviyesini Değiştirmek İstediğinize Emin misiniz?')) {
      $.ajax({
      	data:sendData,
      	success: function(r) { alert(r); },
      	error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert(textStatus);
        }
      });
    }
  }
}

function setUsersLevel() {
  var send=false;
  var sendData='do=userlevel';
  $("input[name='user']:checked").each(function(){
    send=true;
    var user_id=$(this).attr('value');
    sendData=sendData+"&user[]="+user_id;
  });
}

function optionSelect(id,val) {
  $('select[id="'+id+'"]').children('[value="'+val+'"]').attr('selected','selected');
}